//index.js
//获取应用实例
var app = getApp()
var Api = require('../../utils/api.js')

Page({
  data: {
    tabDataList: [],
  },
  update: function () {
    this.setData({
      tabDataList: this.data.tabDataList,
    })
  },
  //事件处理函数  
  onLoad: function () {
    
  },
  onReady: function () {
    // 页面渲染完成
    this.onPullDownRefresh();
  },
  onShow: function () {
    // 页面显示

  },
  onHide: function () {
    // 页面隐藏    
  },
  onUnload: function () {
    // 页面关闭    
  },
  onShareAppMessage: function () {
    return {
      title: '次元君!Cute',
      desc: '二货萌物:"带你看小姐姐.小哥哥!',
      path: '/pages/recommend/recommend'
    }
  },
  onPullDownRefresh: function () {
    //页面相关事件处理函数--监听用户下拉动作
    console.log("页面相关事件处理函数--监听用户下拉动作");
    wx.showLoading({
      title: '小次君:正在努力重新找小姐姐中',
      mask: true,
    });
    this.refresh(function (count) {
      wx.stopPullDownRefresh();
      wx.hideLoading();
      wx.showToast({
        title: '更新' + count + '个',
        duration: 500,
      })
    });
  },
  onReachBottom: function () {
    //页面上拉触底事件的处理函数
    wx.showLoading({
      title: '小次君:正在找更多小姐姐',
      mask: true,
    });
    this.loadMore(function (count) {
      wx.hideLoading();
      wx.showToast({
        title: '更新' + count + '个',
        duration: 500,
      })
    });
  },
  cellClick: function (e) {
    var data = e.currentTarget.dataset;
    var sid = data.item.sid;
    //console.log(data);
    wx.navigateTo({
      //url: '../other/videoDetails/index' + '?sid=' + sid
      url: '/pages/other/videoDetails/index' + '?sid=' + sid
      //url: '../detail/detail'+'?deviceId='+curObj.id
    })
  },
  isHaveSameNews: function (sid) {
    var tmp = false;
    var newslist = this.data.tabDataList;
    for (var key in newslist) {
      var obj = newslist[key];
      if (obj.sid == sid) {
        return true;
      }
    }
    return false;
  },
  loadMore: function (cb) {
    var self = this;
    Api.getNews(function (res) {
      var newlist = res.data.newsList;
      var count = 0;
      for (var key in newlist) {
        var obj = newlist[key];
        if (self.isHaveSameNews(obj.sid) == false) {

          var listObj = {};
          listObj.imageUrl = obj.images[0];
          //listObj.imageUrl = "http://img.blog.csdn.net/20160926230934113";
          listObj.title = obj.title;
          listObj.writer = obj.sid;
          listObj.viewCount = obj.viewCount;
          listObj.date = obj.createTime;
          listObj.sid = obj.sid;
          count = count + 1;
          self.data.tabDataList.push(listObj);
        }

      }
      self.update();
      //console.log(self.data.tabDataList);
      if (typeof cb == "function") {
        cb(count);
      }
    });
  },
  refresh: function (cb) {
    var self = this;
    Api.getNews(function (res) {
      self.data.tabDataList = [];
      var newlist = res.data.newsList;
      var count = 0;
      for (var key in newlist) {
        var obj = newlist[key];

        var listObj = {};
        listObj.imageUrl = obj.images[0];
        //listObj.imageUrl = "http://img.blog.csdn.net/20160926230934113";
        listObj.title = obj.title;
        listObj.writer = obj.sid;
        listObj.viewCount = obj.viewCount;
        listObj.date = obj.createTime;
        listObj.sid = obj.sid;

        count = count + 1;
        self.data.tabDataList.push(listObj);
      }
      self.update();
      if (typeof cb == "function") {
        cb(count);
      }
    });
  },
})
